package top.happylaishop.shop.controller.fontend;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.happylaishop.shop.dto.CartDto;
import top.happylaishop.shop.dto.UpdateCartProductDto;
import top.happylaishop.shop.service.CartService;
import top.happylaishop.shop.service.CommonService;
import top.happylaishop.shop.utils.resp.RestResponse;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/list")
    public RestResponse<CartDto> list(){
        return RestResponse.defaultSucessData(cartService.list(commonService.getCurrentUserInfo().getId()));
    }

    @PostMapping("/add")
    public RestResponse<CartDto> add(@RequestBody UpdateCartProductDto updateCartProductDto){
        updateCartProductDto.setUserId(commonService.getCurrentUserInfo().getId());
        return RestResponse.defaultSucessData(cartService.add(updateCartProductDto));
    }

    @PostMapping("/update")
    public RestResponse<CartDto> update(@RequestBody UpdateCartProductDto updateCartProductDto){
        updateCartProductDto.setUserId(commonService.getCurrentUserInfo().getId());
        return cartService.update(updateCartProductDto);
    }

    @PostMapping("/getCartProductCount")
    public RestResponse<Integer> getCartProductCount(){
        return cartService.getCartProductCount(commonService.getCurrentUserInfo().getId());
    }

    @PostMapping("/selectOrUnSelect/{productId}/{checked}")
    public RestResponse<CartDto> selectOrUnSelect(@PathVariable Integer productId, @PathVariable Integer checked){
        return cartService.selectOrUnSelect(commonService.getCurrentUserInfo(), productId, checked);
    }

    @PostMapping("/deleteProduct/{productIds}")
    public RestResponse<CartDto> deleteProduct(@PathVariable String productIds){
        return cartService.deleteProduct(commonService.getCurrentUserInfo(), productIds);
    }

    @PostMapping("/changeAllSelectedStatus/{checked}")
    public RestResponse<CartDto> changeAllSelectedStatus(@PathVariable Integer checked){
        return cartService.changeAllSelectedStatus(commonService.getCurrentUserInfo(), checked);
    }
}
